<template>
	<view class="shopping">
		<view class="shopping_top">
			购物车
			<text class="shopping_management" v-show="!cartCheaked && goodsList.length" @click="handleManageGoods">管理</text>
			<text class="shopping_management" @click="handleManageGoods"  v-show="cartCheaked">取消</text>
		</view>
		<view class="shopping_main" v-if="goodsList.length">
			<view class="shopping_cart" v-for="(item, i) in goodsList" :key="i">
				<view class="shopping_cart_commodity_info">
					<view class="commodity_image">
						<image :src="'http://gy.hyq.ink/' + item.image" mode=""></image>
					</view>
					<view class="commodity_synopsis">
						<view class="commodity_title">
							{{item.goods_name}}
						</view>
						<view class="shopping_cart_timer_money">
							<view class="shopping_cart_timer">
								付款倒计时: <text>{{item.lastPayTime}}</text>
							</view>
							<view class="shopping_cart_money">
								¥{{item.price}}
							</view>
						</view>
					</view>
				</view>
				<view class="shopping_cart_btn">
					<slot name="footer"></slot>
					<view class="shopping_cart_checked_btn" v-show="cartCheaked">
						<image src="@/static/image/shoppingUnselected.png" mode=""></image>
					</view>
					<view class="shopping_cart_order_btn">
						<text class="shopping_cart_order_cancel" @click="handleCancelOrder(item.id)">
							取消订单
						</text>
						<text class="shopping_cart_order_payment" @click="handleToPayGoods(item.id)">
							立即付款
						</text>
					</view>
				</view>
		</view>
			<view class="shopping_cart_bottom_position">
				<view class="shopping_cart_bottom_payment">
					<view class="shopping_cart_bottom_checked">
						<image src="@/static/image/shoppingUnselected.png" mode=""></image>
						<text>删除</text>
					</view>
					<view class="shopping_cart_bottom_payment_btn">
						删除
					</view>
				</view>
			</view>
		</view>
		<view class="none_goods tc" v-else>
			<image src="@/static/image/noneGoods.png" mode=""></image>
			<view class="none_goods_tips">
				购物车竟然是空的
			</view>
			<view class="none_goods_btns mg-lr-c" @click="handleToHomePage">
				去首页逛逛
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		components: {
		},
		data() {
			return {
				cartCheaked: false, // 是否显示选择框
				goodsList: [], // 购物车商品列表
				timer: null,
			}
		},
		onLoad() {
			this.getShopCartList();
		},
		methods: {
			/** 跳转至支付页面 */
			handleToPayGoods(orderId) {
				uni.navigateTo({
					url: `../setting/payment/index?id=${orderId}`
				})
			},
			/** 展示商品选择框 */
			handleManageGoods() {
				this.cartCheaked = !this.cartCheaked;
			},
			/** 跳转至首页 */
			handleToHomePage() {
				uni.switchTab({
					url: '../index/index'
				})
			},
			/** 获取购物车列表 */
			getShopCartList() {
				let parameter = {
					page: 1, // 页数
					status: 1, // 状态 1: 待支付
				};
				this.$post(this.api.orderList, parameter, 'post', { token: uni.getStorageSync('token') }).then((res) => {
					for(let i = 0; i < res.data.list.length; i++) {
						res.data.list[i].lastPayTime = '';
					}
					this.goodsList = res.data.list;
					this.goodsList.forEach((item, i) => {
						let self = this;
						/** 生成订单时间戳 */
						let createTime = item.create_time;
						/** 结束时间戳 */
						let endTime = createTime + 30000;
						this.timer = setInterval(function() {
							 /** 当前时间时间戳 */
							 let clientTime = new Date().getTime() / 1000;
							 /** 剩余时间时间戳 */
							 let lastTime = endTime - clientTime; 
							 let int_minute;
							if(lastTime > 0){
								int_minute = Math.floor(lastTime / 60);
								lastTime -= int_minute * 60;
								item.lastPayTime = int_minute+'分'+ Math.floor(lastTime) +'秒'
							}else {
								clearInterval(this.timer);
							}
						},1000); 
					})
				}).catch((err) => {
					console.log(err)
				})
			},
			/** 取消订单 */
			handleCancelOrder(id) {
				let parameter = {
					order_id: id
				};
				this.$post(this.api.cancelOrder, parameter, 'post', { token: uni.getStorageSync('token') }).then((res) => {
					uni.showToast({
						title: '订单取消成功',
						icon: 'none',
						duration: 3000,
						position: 'bottom'
					});
					this.getShopCartList();
				}).catch((err) => {
					console.log(err)
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	@import "@/asset/less/base.less"; 
	.shopping{
		.shopping_top{
			width: 100%;
			height: 96rpx;
			background: #D1A764;
			text-align: center;
			line-height: 96rpx;
			color: #F4F5FA;
			font-weight: 500;
			font-size: 36rpx;
			font-family: Source Han Sans CN;
			position: relative;
			.shopping_management{
				position: absolute;
				right: 44rpx;
				font-size: 30rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				float: right;
			}
		}
		.shopping_cart_card {
			padding: 8rpx 20rpx;
		}
		.shopping_cart_bottom_position {
			position: fixed;
			bottom: 120rpx;
			width: 100%;
			height: 80rpx;
			line-height: 80rpx;
			.shopping_cart_bottom_payment {
				box-sizing: border-box;
				width: 100%;
				padding-left: 35rpx;
				display: flex;
				justify-content: space-between;
				.shopping_cart_bottom_checked {
					image {
						height: 35rpx;
						width: 35rpx;
						vertical-align: text-bottom 
					}
					text {
						color: #666666;
						font-size: 30rpx;
						padding-left: 24rpx;
					}
				}
				.shopping_cart_bottom_payment_btn {
					background-color: #D2A967;
					color: #fff;
					font-size: 26rpx;
					width: 198rpx;
					text-align: center;
				}
			}
		}
		.shopping_cart_btn {
			padding-top: 30rpx;
			height: 104rpx;
			box-sizing: border-box;
		    .shopping_cart_checked_btn {
				display: inline-block;
				image {
					width: 35rpx;
					height: 35rpx;
					vertical-align: middle;
				}
			}
			.shopping_cart_order_btn {
				float: right;
				text {
					font-size: 24rpx;
					text-align: center;
					border-radius: 5rpx;
					display: inline-block;
				}
				text:first-child {
					height: 46rpx;
					width: 136rpx;
					line-height: 46rpx;
					color: #999999;
					border: 1rpx solid #E5E5E5;
					margin-right: 20rpx;
				}
				text:last-child {
					color: #F4F5FA;
					height: 48rpx;
					width: 138rpx;
					line-height: 48rpx;
					background: linear-gradient(90deg, #CFA461 0%, #DCBB7E 100%);
				}
			}
		}
	}
	.none_goods {
		margin-top: 246rpx;
		font-size: 26rpx;
		image {
			width: 211rpx;
			height: 192rpx;
		}
		.none_goods_tips {
			padding: 40rpx 0;
			color: #969494;
		}
		.none_goods_btns {
			color: #fff;
			width: 206rpx;
			height: 60rpx;
			line-height: 60rpx;
			background: #D1A764;
			border-radius: 30rpx;
		}
	}
	.shopping_cart {
		margin-top: 22rpx;
		border-radius: 10px;
		padding: 18rpx 18rpx 0rpx 18rpx;
		box-shadow: 0px 0px 20px #E8E8E8;
		.shopping_cart_commodity_info {
			display: flex;
			justify-content: space-between;
			padding-bottom: 22rpx;
			border-bottom: 1rpx solid #E5E5E5;
		}
		.commodity_image {
			margin-right: 20rpx;
			width: 240rpx;
			height: 170rpx;
			image {
				width: 100%;
				height: 100%;
			}
		}
		.commodity_synopsis {
			width: 100%;
			.commodity_title {
				min-height: 65rpx;
				padding-top: 8rpx;
				line-height: 36rpx;
				color: #2D2D2D;
				padding: 9rpx 23rpx 0 0;
				font-size: 30rpx;
				overflow: hidden;
				-webkit-line-clamp: 2;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-box-orient: vertical;
			}
		}
		.shopping_cart_timer_money {
			padding-top: 39rpx;
			display: flex;
			justify-content: space-between;
			.shopping_cart_money {
				font-size: 30rpx;
				padding-top: 8rpx;
			}
			.shopping_cart_timer {
				background-color: #FFF1D1;
				color: #D2A967;
				font-size: 24rpx;
				text-align: center;
				padding: 10rpx 12rpx;
				border-radius: 5rpx;
			}
		}
		.shopping_cart_btn {
			padding-top: 30rpx;
			height: 104rpx;
			box-sizing: border-box;
	        .shopping_cart_checked_btn {
				display: inline-block;
				image {
					width: 35rpx;
					height: 35rpx;
					vertical-align: middle;
				}
			}
			.shopping_cart_order_btn {
				float: right;
				text {
					font-size: 24rpx;
					text-align: center;
					border-radius: 5rpx;
					display: inline-block;
				}
				text:first-child {
					height: 46rpx;
					width: 136rpx;
					line-height: 46rpx;
					color: #999999;
					border: 1rpx solid #E5E5E5;
					margin-right: 20rpx;
				}
				text:last-child {
					color: #F4F5FA;
					height: 48rpx;
					width: 138rpx;
					line-height: 48rpx;
					background: linear-gradient(90deg, #CFA461 0%, #DCBB7E 100%);
				}
			}
		}
	}
</style>